package com.xiaren.ui.dialog;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.xiaren.bean.SelectText;
import com.xiaren.common.ObjectConstant;
import com.xiaren.ui.edit.FileEdit;

/**
 * 搜索文件的对话框
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 *
 */
public class SearchDialog extends JDialog implements ActionListener {
	private FileEdit fileEdit;

	public SearchDialog(JFrame arg0, String arg1, boolean arg2,
						FileEdit fileEdit) {
		super(arg0, arg1, arg2);
		this.fileEdit = fileEdit;
		init();
	}

	JTextField inputContent = new JTextField();
	JPanel radioPanel = new JPanel();
	// ButtonGroup grpSelect = new ButtonGroup();
	// JRadioButton radAll = new JRadioButton("Man", true);
	// JRadioButton radFor = new JRadioButton("Woman");

	JCheckBox caseSensitive = new JCheckBox("Case Sensitive", true);
	JButton btnFind = new JButton("查找");
	JButton btnClose = new JButton("关闭");

	private void init() {
		this.setBounds(ObjectConstant.screenWidth >> 1,
				ObjectConstant.screenHeight >> 1, 300, 120);
		this.setLayout(null);
		this.radioPanel.setLayout(null);
		inputContent.setBounds(25, 15, 250, 25);
		// radioPanel.setBounds(0, 50, 300, 30);
		// caseSensitive.setBounds(5, 5, 150, 20);
		btnFind.setBounds(50, 60, 75, 20);
		btnClose.setBounds(165, 60, 75, 20);

		this.add(inputContent);
		// radioPanel.add(caseSensitive);
		// this.add(radioPanel);
		this.add(btnFind);
		this.add(btnClose);

		this.setVisible(true);
		btnFind.addActionListener(this);
		btnClose.addActionListener(this);
		inputContent.addActionListener(this);
	}

	public void setInput(String input) {
		this.inputContent.setText(input);
	}

	public JTextField getInputContent() {
		return inputContent;
	}

	/**
	 * 创建新的查询窗体（单例）
	 *
	 * @param selectContent
	 * @param fileEdit
	 */
	public static void createFindWindow(SelectText selectContent,
										FileEdit fileEdit) {
		if (ObjectConstant.findDialog == null) {
			ObjectConstant.findDialog = new SearchDialog(
					ObjectConstant.mainFrame, "查找", false, fileEdit);
			// System.out.println(ObjectConstant.findDialog);
		} else {
			ObjectConstant.findDialog.fileEdit = fileEdit;
			ObjectConstant.findDialog.setVisible(true);
		}
		// ObjectConstant.findDialog.setFocusable(true);
		// ObjectConstant.findDialog.inputContent.setFocusable(true);
		ObjectConstant.findDialog.setInput(selectContent.getContent());
		ObjectConstant.findDialog.getInputContent().setSelectionStart(0);
		ObjectConstant.findDialog.getInputContent().setSelectionEnd(
				selectContent.getEnd() - selectContent.getBegin());

	}

	/**
	 * 根据目标文件的内容，和搜索的文件内容得到搜索的结果 (不区分大小写)
	 *
	 * @param searchText
	 * @param source
	 * @return
	 */
	public SelectText findFile(String searchText, String source, int fromIndex) {
		source = source.replaceAll("\r", "");// 去掉\r是因为jtextpane里面没有\r
		int begin = source.toUpperCase().indexOf(searchText.toUpperCase(),
				fromIndex + 1);
		if (begin == -1) {// 如果到最后没找到，就回到开始
			begin = source.toUpperCase().indexOf(searchText.toUpperCase(), 0);
			if (begin == -1) {// 从开始没找到的话，就真的没知道了
				return new SelectText();
			}
		}
		int end = begin + searchText.length();
		// System.out.println("fromIndex = " + fromIndex + " , begin = "
		// + begin);
		return new SelectText(begin, end);

	}

	@Override
	public void actionPerformed(ActionEvent action) {
		if (action.getSource() == this.btnClose) {
			this.setVisible(false);
		} else if (action.getSource() == this.btnFind) {// 查询操作
			findContent();
		} else if (action.getSource() == this.inputContent) {
			findContent();
		}
	}
	/**
	 * 获取当前活动的文本，然后进行搜索
	 */
	private void findContent() {
		ObjectConstant.findDialog.fileEdit = (FileEdit) ObjectConstant.mainEdit.getSelectedComponent();
		int fromIndex = this.fileEdit.getSelectFile().getBegin();
		SelectText selectText = findFile(this.inputContent.getText(),
				fileEdit.getFileContent(), fromIndex);
		this.fileEdit.setSelectFile(selectText);
	}

	public static void main(String[] args) {
		String ss = "abcdefgaccabbaewwdefefqwefqwea";
		// System.out.println(ss.indexOf("def", 4));
		JPasswordField pwd = new JPasswordField();
//		String[] options = {"确订", "取消"};
//		int result = JOptionPane.showOptionDialog(null, pwd, "请输入密码", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, null);
//		System.out.println(result);

//		JOptionPane.showInputDialog(null, pwd,"title",0,null,new Object[]{"aaa","bbb"}, "cccc");

		JOptionPane.showMessageDialog(null, pwd);


	}

}
